package com.zeng.ssm.service.support.trans.server.base;

import cn.hutool.core.util.CharsetUtil;
import com.zeng.ssm.dal.model.AccountLog;
import com.zeng.ssm.dal.model.AccountMsgLog;
import com.zeng.ssm.service.support.trans.GxpConstant;
import com.zeng.ssm.service.support.trans.base.BaseServerHandler;
import com.zeng.ssm.service.support.trans.base.model.BaseMessage;
import com.zeng.ssm.service.support.trans.base.model.Head;
import com.zeng.ssm.service.support.trans.base.model.IBody;
import com.zeng.ssm.service.support.trans.base.model.Ret;
import com.zeng.ssm.util.XStreamUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

/**
 * GXP服务端-Base类
 *
 * @author zengwanlin
 * @date 2023-09-04T13:29
 */
public abstract class BaseGxpServerHandler extends BaseServerHandler {

    /**
     * 解析请求报文
     *
     * @return lock_key，用于加锁，为null时不加锁
     */
    @Override
    protected String parseRequestMsg() {
        BaseMessage reqMessage = XStreamUtil.fromXml(component.getReqMsg(), BaseMessage.class, IBody.class, getRequestBodyClass());
        component.setReqSeqNo(reqMessage.getHead().getReqSeqNo());
        component.setReqMessage(reqMessage);
        return initRequestParam();
    }

    /**
     * 判断交易是否已成功
     *
     * @param accountLog    根据请求流水号查询到的交易记录
     * @param accountMsgLog 根据请求流水号查询到的交易报文记录
     * @return true-已成功，false-未成功
     */
    @Override
    protected boolean isTranSucceed(AccountLog accountLog, AccountMsgLog accountMsgLog) {
        throw new IllegalStateException(accountLog.getId() + "-未知的交易状态，请联系人工处理");
    }

    /**
     * 判断交易是否已成功
     * <p>
     * 根据流水号查询不到交易记录
     *
     * @return true-已成功，false-未成功
     */
    @Override
    protected boolean isTranSucceed() {
        throw new IllegalStateException("未知的交易状态，请联系人工处理");
    }

    /**
     * 组装响应报文
     */
    @Override
    protected void buildResponseMsg(IBody responseBody) {
        // 组装报文头
        Head head = buildHead();
        // 请求报文
        BaseMessage message = new BaseMessage();
        message.setHead(head);
        message.setBody(responseBody);
        String respMsg = XStreamUtil.toXml(message);
        respMsg = String.format("%08d", respMsg.getBytes(CharsetUtil.CHARSET_UTF_8).length) + respMsg;

        component.setRespSeqNo(component.getReqSeqNo());
        component.setRespMsg(respMsg);
    }

    /**
     * 获取请求实体类Class
     *
     * @return
     */
    protected abstract Class<? extends IBody> getRequestBodyClass();

    /**
     * 初始化请求参数
     * <p>
     * 设置ContractId, DetailId, ProductNo等
     *
     * @return lock_key，用于加锁，为null时不加锁
     */
    protected abstract String initRequestParam();

    /**
     * 组装报文头
     *
     * @return
     */
    protected Head buildHead() {
        Head head = new Head();
        BeanUtils.copyProperties(component.getReqMessage().getHead(), head);
        head.setRespSeqNo(component.getReqSeqNo());
        Ret ret = new Ret();
        if (component.isSuccess()) {
            ret.setRetCode(GxpConstant.RET_CODE_0000);
            ret.setRetMsg(GxpConstant.RET_MSG_SUCCESS);
        } else {
            ret.setRetCode(GxpConstant.RET_CODE_9999);
            ret.setRetMsg(StringUtils.defaultString(component.getRetMsg(), GxpConstant.RET_MSG_FAIL));
        }
        head.setRet(ret);
        return head;
    }

}
